<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 生成缩略图
 * @param  $imgurl 图片路径
 * @param  $width  缩略图宽度
 * @param  $height 缩略图高度
 * @param  $nopic 无图片是默认图片路径
 */
function thumb($src, $width=100, $height=100, $noimg='not.jpg') {

    $sourcepath = config('upload.path').$src;
    $imgurl = config('upload.imgurl').dirname($src)."/";

    if(empty($src) || !file_exists($sourcepath)){
        return '/static/'.$noimg;
    }

    $thumbpath = dirname($sourcepath)."/";
    $imgname = basename($src);
    $ext = strtolower(strrchr($imgname, "."));
    $imgname = ($ext === FALSE) ? $imgname : substr($imgname, 0, -strlen($ext));
    $filename = $imgname."_".$width."_".$height.$ext;

    if(file_exists($thumbpath.$filename)){
        return $imgurl.$filename;
    }

    $image = \think\Image::open($sourcepath);
    $image->thumb($width, $height, \think\Image::THUMB_CENTER)->save($thumbpath.$filename);

    return $imgurl.$filename;
}
